#include "./../inc/page_manager.h"

PageManager *PageManager::m_instance = nullptr;

PageManager::~PageManager()
{
    for (auto page : pages)
    {
        delete page;
    }
    pages.clear();
}
PageManager *PageManager::instance()
{
    if (m_instance == nullptr)
    {
        m_instance = new PageManager();
    }
    return m_instance;
}

void PageManager::delInstance()
{
    delete m_instance;
    m_instance = nullptr;
}

// 添加页面
void PageManager::addPage(BasePage *page)
{
    if (page != nullptr)
    {
        pages.push_back(page);
    }
}

// 切换到指定页面
void PageManager::switchPage(BasePage *page)
{
    if (current_page != nullptr)
    {
        current_page->hide();
    }
    current_page = page;
    if (current_page != nullptr)
    {
        current_page->show();
    }
}

// 处理触摸事件（转发给当前页面）
bool PageManager::handleTouchEvent(const Point &touch_point)
{
    if (current_page != nullptr)
    {
        return current_page->handleTouch(touch_point);
    }
    return false;
}

// 获取当前页面
BasePage *PageManager::getCurrentPage()
{
    return current_page;
}
